import Vue from 'vue'
import Router from 'vue-router'
import HomePage from '../components/HomePage.vue'
import OrderPage from '../components/OrderPage.vue'
import InfoPage from '../components/UserInfoPage.vue'
import CartPage from '../components/CartPage.vue'
import SearchPage from '../components/SearchPage.vue'
import LoginPage from '../components/LoginPage.vue'
import RegisterPage from '../components/RegisterPage.vue'
import SearchingPage from '../components/SearchingPage.vue'
import LotteryPage from '../components/LotteryPage.vue'
import OrderDetailPage from '../components/OrderDetailPage.vue'

const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

// replace
const originalReplace = Router.prototype.replace
Router.prototype.replace= function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomePage,
    },
    {
      path:'/order',
      name:'order',
      component:OrderPage
    },
    {
      path:'/info',
      name:'info',
      component:InfoPage
    },
    {
      path: '/cart',
      name: 'cart',
      component: CartPage
    },
    {
      path:'/searching',
      name:'searching-all',
      component:SearchingPage
    },
    {
      path:'/searching/:keyword',
      name:'searching',
      component:SearchingPage
    },
    {
      path:'/search',
      name:'search-all',
      component:SearchPage
    },
    {
      path:'/search/:keyword',
      name:'search',
      component:SearchPage
    },
    {
      path:'/login',
      name:'login',
      component:LoginPage
    },
    {
      path:'/register',
      name:'register',
      component:RegisterPage
    },
    {
      path:'/lottery',
      name:'lottery',
      component:LotteryPage
    },
    {
      path:'/orderDetail',
      name:'orderDetail',
      component:OrderDetailPage
    }
  ]
})

router.beforeEach((to,from,next)=>{
  let store = this.a.app.$store

  switch(to.name) {
    case 'login':
    case 'register': {
      next()
      return
    }

    /* 搜索界面刷新重定向 */
    case 'search':
    case 'search-all': {
      console.log('重定向 searching')
      if (store.state.search == null) {
        let keyword = to.params.keyword
        next({ path:'/searching/' + (keyword != null ? keyword : '') })
        return
      }
      break
    }
    case 'cart':
    case 'lottery':
    case 'order':
    case 'info': {
      if (store.state.user == null) {
        store.dispatch('tryAutoLogin', {
          onResponse: function () {
            console.log('成功')
            next()
          },
          onFailure: function () {
            console.log('失败')
            alert("请先进行登录")
            try {
              /* 拦截并重定向至登录界面 */
              next({ name: 'login' })

              /* 消除 tab 激活状态问题 */
              setTimeout(function () {
                $('#tab-controller-login').tab('show')
              }, 100)
            } catch (e) {
              console.log("???????啊这")
            }
          }
        })
        return
      }
      break
    }
  }

  next()
})

router.afterEach((to, from) => {
  console.log('[router.afterEach] to.name: ' + to.name)
  $('#tab-controller-' + to.name).tab('show')
})

export default router
